/**
 * Copyright (C) 2011 KingCore Science & Technology CO,.LTD. All rights reserved.
 * XNS PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

/**
 * Copyright (C) 2002-2011 WUZEWEN. All rights reserved.
 * WUZEWEN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 * This software is the confidential and proprietary information of
 * WuZeven, Personal. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with WuZeven.
 */

package com.kingcore.director.core.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.kingcore.director.DirectorConstants;
import com.kingcore.director.base.dao.impl.DirectorDaoJdbcImpl;
import com.kingcore.director.core.dao.DirectorCoreDao;
import com.kingcore.framework.context.ApplicationContext;

/**
 * <p>java类文件的说明...</p>
 * @author Zeven/wzw on Jul 17, 2011
 * @version	1.0
 * @see		Object#equals(java.lang.Object)
 * @since	JDK5
 */

public class DirectorCoreDaoImpl extends DirectorDaoJdbcImpl implements
		DirectorCoreDao {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * <p>java方法的说明...</p>
	 * @author Zeven on Jul 17, 2011
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}
	
	public String getInitParameterFromDatabase(String key, String defaultValue) throws SQLException{
//		return getInitParameterFromDatabase(key, defaultValue, 
//							DirectorConstants.SYS_Setting, "KEY","VAL");

		Connection conn= null;
		Statement stmt =null;
		ResultSet rs=null;
		String strReturn = defaultValue;
		try {
			conn= ApplicationContext.getInstance().getDataSourceManager().getConnection();
			stmt = conn.createStatement();
			conn.setAutoCommit(true);
			// 界面风格
			rs = stmt.executeQuery("SELECT VAL FROM "+DirectorConstants.SYS_Setting+" WHERE KEY='"+ key +"'");
			if(rs.next()){
				strReturn = rs.getString("VAL");
			}else{
				stmt.executeUpdate("Insert Into "+DirectorConstants.SYS_Setting+"(Key,Val) values('"+key+"','"+defaultValue+"')");
				rs=null; // 否则Access2000下再关闭时候报异常：java.sql.SQLException: ResultSet is closed
			}				
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		} finally {
			wzw.util.DbUtils.closeQuietly(conn, stmt, rs);
		}
		return strReturn;
	}
	
	/**
	 * 
	 * <p>java方法的说明...</p>
	 * @author Zeven on Jul 17, 2011
	 * @param key
	 * @param defaultValue 如果不存在时候的缺省值
	 * @return
	 * @throws Exception 
	 */
	public String getInitParameterFromDatabase(String key, String defaultValue,
			                         String tblName, String keyCol, String valCol) throws SQLException{
		
		
		Connection conn= null;
		Statement stmt =null;
		ResultSet rs=null;
		String strReturn = defaultValue;
		try {
			conn= ApplicationContext.getInstance().getDataSourceManager().getConnection();
			stmt = conn.createStatement();
			conn.setAutoCommit(true);
			// 界面风格
			rs = stmt.executeQuery("SELECT "+valCol+" FROM "+tblName+" WHERE "+keyCol+"="+ key );
			if(rs.next()){
				strReturn = rs.getString(1); //the first column is 1
			}else{
				stmt.executeUpdate("Insert Into "+tblName+"("+keyCol+","+valCol+") values('"+key+"','"+defaultValue+"')");
				rs=null; // 否则Access2000下再关闭时候报异常：java.sql.SQLException: ResultSet is closed
			}				
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		} finally {
			wzw.util.DbUtils.closeQuietly(conn, stmt, rs);
		}
		return strReturn;
	}

	public void updateInitParameterToDatabase(String key, String newValue){
		Connection conn= null;
		Statement stmt =null;
		try {
			conn= ApplicationContext.getInstance().getDataSourceManager().getConnection();
			stmt = conn.createStatement();
			conn.setAutoCommit(true);
			stmt.executeUpdate("UPDATE "+DirectorConstants.SYS_Setting+" SET VAL='"+newValue+"' Where key='"+key+"'");
							
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			wzw.util.DbUtils.closeQuietly(conn, stmt, null);
		}
	}
}
